package blue_bridge_cup_learning.data_struct_code;

public class Queue_Link {
    private Node front;
    private Node rear;

    public class Node {
        public int value;
        public Node next;

        public Node(int value) {
            this.value = value;
            this.next = null;
        }
    }

    public Queue_Link() {
        this.front = null;
        this.rear = null;
    }

    public boolean isEmpty() {
        return this.front == null;
    }

    public void Enqueue(int value) {
        Node newNode = new Node(value);
        // 将front和rear指向同一个节点建立连接
        if (this.isEmpty()) {
            this.front = newNode;
            this.rear = newNode;
        } else {
            // 将rear的next指向新的节点，后移rear
            this.rear.next = newNode;
            this.rear = newNode;
        }

    }

    public int Dequeue() {
        if (this.isEmpty()) {
            System.out.println("队列为空，无法出队");
            return -1;
        }
        int value = this.front.value;
        this.front = this.front.next;
        return value;
    }

    public void PrintQueue() {
        Node current = this.front;
        while (current != null) {
            System.out.println(current.value);
            current = current.next;
        }
    }

    public static void main(String[] args) {
        Queue_Link queue_link = new Queue_Link();
        queue_link.Enqueue(1);
        queue_link.Enqueue(2);
        queue_link.Enqueue(3);
        queue_link.Dequeue();
        queue_link.PrintQueue();
    }

}
